<script setup lang="ts">
import { ref, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import BaseImage from '~/components/BaseImage.vue'
import closeIcon from '/images/webp/notification/close_icon.webp'
import plusIcon from '/images/webp/notification/upload_img.webp'

const { t } = useI18n()

interface FeedbackItem {
  id: string;
  time: string;
  status: '待处理' | '已完成';
  content: string;
}

const props = defineProps<{
  activeTab: number;
}>();

const emit = defineEmits<{
  (e: 'tab-change', index: number): void;
  (e: 'open-chat', feedback: FeedbackItem): void;
}>();

const activeFeedbackTab = ref(0);
const feedbackContent = ref('');
const feedbackImages = ref<string[]>([]);
const maxImagesCount = 5;
const maxContentLength = 200;
const feedbackCharCount = computed(() => feedbackContent.value.length);

const feedbackTabs = [
  { name: t('notification_create_feedback') },
  { name: t('notification_my_feedback') }
];

const myFeedbacks = ref<FeedbackItem[]>([]);

const handleFeedbackTabChange = (index: number) => {
  activeFeedbackTab.value = index;
  emit('tab-change', index);
};

const uploadImage = () => {
  if (feedbackImages.value.length >= maxImagesCount) {
    return;
  }
  feedbackImages.value.push('https://via.placeholder.com/100');
};

const removeImage = (index: number) => {
  feedbackImages.value.splice(index, 1);
};

const submitFeedback = () => {
  if (!feedbackContent.value.trim()) {
    return;
  }
  feedbackContent.value = '';
  feedbackImages.value = [];
  activeFeedbackTab.value = 1;
  emit('tab-change', 1);
};

const cancelFeedback = () => {
  feedbackContent.value = '';
  feedbackImages.value = [];
};

const openFeedbackChatModal = (feedback: FeedbackItem) => {
  emit('open-chat', feedback);
};
</script>

<template>
  <div class="px-[16px]">
    <!-- 子标签页 -->
    <div class="feedback-tabs">
      <div v-for="(tab, index) in feedbackTabs" :key="index" class="feedback-tab"
        :class="{ active: activeFeedbackTab === index }" @click="handleFeedbackTabChange(index)">
        {{ tab.name }}
      </div>
    </div>

    <!-- 创建反馈内容 -->
    <div v-if="activeFeedbackTab === 0" class="feedback-content">
      <div class="feedback-section">
        <div class="feedback-title">
          <span>{{ t('notification_feedback_content') }}</span>
          <div class="feedback-char-count">{{ feedbackCharCount }}/{{ maxContentLength }}</div>
        </div>

        <textarea v-model="feedbackContent" class="feedback-textarea"
          :placeholder="t('notification_enter_feedback_content')" :maxlength="maxContentLength"></textarea>
      </div>

      <div class="feedback-section">
        <div class="feedback-title">{{ t('notification_image_upload') }}</div>
        <div class="feedback-images">
          <div v-for="(image, index) in feedbackImages" :key="index" class="feedback-image-item">
            <img :src="image" alt="反馈图片" />
            <div class="feedback-image-delete" @click="removeImage(index)">
              <BaseImage width="56px" :url="closeIcon" />
            </div>
          </div>
          <div v-if="feedbackImages.length < maxImagesCount" class="feedback-image-upload" @click="uploadImage">
            <BaseImage width="56px" :url="plusIcon" />
          </div>
        </div>
        <div class="feedback-image-hint">{{ t('notification_upload_image_hint') }}</div>
      </div>

      <div class="feedback-section">
        <div class="feedback-title" style="font-size: 16px;">{{ t('notification_reward_rules') }}</div>
        <div class="feedback-rules">
          {{ t('notification_feedback_reward_description') }}
        </div>
      </div>

      <div class="feedback-actions">
        <button class="feedback-cancel" @click="cancelFeedback">{{ t('cancel') }}</button>
        <button class="feedback-submit" @click="submitFeedback">{{ t('submit') }}</button>
      </div>
    </div>

    <!-- 我的反馈列表 -->
    <div v-else-if="activeFeedbackTab === 1" class="feedback-list">
      <div v-for="feedback in myFeedbacks" :key="feedback.id" class="feedback-item"
        @click="openFeedbackChatModal(feedback)">
        <div class="feedback-time">
          {{ feedback.time }}
          <span class="unread-indicator"></span>
        </div>
        <div class="feedback-status-row">
          <span class="text-white text-[14px]">{{ t('notification_feedback') }}{{ t('notification_status') }}：</span>
          <span class="feedback-status"
            :class="{ 'status-pending': feedback.status === '待处理', 'status-completed': feedback.status === '已完成' }">
            {{ feedback.status }}
          </span>
        </div>
        <div class="feedback-id-row">
          <span>{{ t('notification_feedback') }}ID：</span>
          <span>{{ feedback.id }}</span>
        </div>
        <div class="feedback-content-row">
          <span>{{ t('notification_content') }}：</span>
          <span>{{ feedback.content }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.feedback-tabs {
  display: flex;
  background-color: #232626;
  margin-bottom: 20px;
}

.feedback-tab {
  padding: 15px 20px;
  font-size: 12px;
  color: #B3BEC1;
  cursor: pointer;
  position: relative;
}

.feedback-tab.active {
  color: #ffffff;
}

.feedback-tab.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #24EE89;
}

.feedback-content {
  padding: 12px;
  color: #fff;
  background: #323738;
  border-radius: 6px;
}

.feedback-section {
  margin-bottom: 20px;
}

.feedback-title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 10px;
  padding: 6px 0;
  display: flex;
  justify-content: space-between;
}

.feedback-char-count {
  font-size: 12px;
  color: #B3BEC1;
  text-align: right;
  margin-bottom: 5px;
}

.feedback-textarea {
  width: 100%;
  height: 120px;
  background-color: #292d2e;
  border: none;
  border-radius: 6px;
  padding: 12px;
  color: #B3BEC1;
  font-size: 14px;
  resize: none;
  border-top: 1px solid var(---border-black-33B4041, #3B4041)
}

.feedback-textarea::placeholder {
  color: #7A8283;
}

.feedback-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

.feedback-image-item {
  width: 80px;
  height: 80px;
  position: relative;
  border-radius: 4px;
  overflow: hidden;
}

.feedback-image-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.feedback-image-delete {
  position: absolute;
  top: 5px;
  right: 5px;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.feedback-image-upload {
  background-color: #323738;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.feedback-image-hint {
  font-size: 12px;
  color: #A8AEB0;
  margin-top: 10px;
}

.feedback-rules {
  background-color: #323738;
  border-radius: 6px;
  font-size: 12px;
  color: #A8AEB0;
  line-height: 1.5;
}

.feedback-actions {
  display: flex;
  gap: 12px;
  margin-top: 30px;
}

.feedback-cancel {
  flex: 1;
  height: 44px;
  background-color: #3A4142;
  border: none;
  border-radius: 8px;
  color: #B3BEC1;
  font-weight: bold;
  cursor: pointer;
  font-size: 12px;
}

.feedback-submit {
  flex: 1;
  height: 44px;
  background: linear-gradient(90deg, #24EE89 0%, #9FE871 100%);
  border: none;
  border-radius: 8px;
  color: #000;
  font-weight: bold;
  cursor: pointer;
  font-size: 12px;
}

.feedback-list {
  overflow-y: auto;
  flex: 1;
}

.feedback-item {
  background-color: #323738;
  border-radius: 6px;
  margin-bottom: 12px;
  padding: 12px;
  position: relative;
  cursor: pointer;
}

.feedback-time {
  color: #B3BEC1;
  font-size: 12px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
}

.feedback-status-row,
.feedback-id-row,
.feedback-content-row {
  margin-bottom: 6px;
  font-size: 12px;
  color: #B3BEC1;
}

.feedback-status {
  font-size: 14px;
  font-weight: bold;
}

.status-pending {
  color: #24EE89;
}

.status-completed {
  color: #ffffff;
}

.unread-indicator {
  display: inline-block;
  width: 8px;
  height: 8px;
  background-color: #24EE89;
  border-radius: 50%;
  margin-left: 8px;
}
</style> 